/*
 * @Description: ------------ fileDescription -----------
 * @Author: snows_l snows_l@163.com
 * @Date: 2024-03-24 10:22:55
 * @LastEditors: snows_l snows_l@163.com
 * @LastEditTime: 2025-01-20 11:17:03
 * @FilePath: \BLOG\src\store\app.js
 */
import { getMusicList } from '@/api/music';
import { projectDefaultConfig } from '@/config/defaultConfig';
import { defineStore } from 'pinia';
import { getDark } from '@/utils/theme';
export const useAppStore = defineStore('app', {
  state: () => {
    return {
      isDark: getDark() === 'dark' ? 'dark' : 'light',
      articleDict: [],
      musicDict: [],
      musicList: [],
      isFullSreen: false,
      bgIndex: 50,
      isAutoToggleBg: true,
      toggleBgTime: 5, // toggleBgTime 单位：分钟
      projectConfig: { ...projectDefaultConfig }
    };
  },

  actions: {
    // 设置主题
    SET_DARK(payload) {
      this.isDark = payload;
    },
    // 设置文章字典
    SET_ARTICLE_DICT(payload) {
      this.articleDict = payload;
    },
    // 设置音乐字典
    SET_MUSIC_DICT(payload) {
      this.musicDict = payload;
    },
    // 设置音乐列表
    SET_MUSIC_LIST(payload) {
      this.musicList = payload;
    },
    // 设置全屏状态
    SET_FULL_SCREEN(payload) {
      this.isFullSreen = payload;
    },
    // 设置当前背景图索引
    SET_BG_INDEX(payload) {
      this.bgIndex = payload;
    },
    SET_AUTO_TOGGLE_BG(payload) {
      this.isAutoToggleBg = payload;
    },
    SET_TOGGLE_BG_TIME(payload) {
      this.toggleBgTime = payload;
    },
    SET_PROJECTCONFIG(payload) {
      let paramskeys = Object.keys(payload);
      for (let key of paramskeys) {
        this.projectConfig[key] = payload[key] ? payload[key] : projectDefaultConfig[key];
      }
    },

    // 获取所有的音乐列表
    GET_MUSIC_LIST() {
      let serverUrl = this.projectConfig.bsServerUrl;
      getMusicList({ isUnPage: false }).then(res => {
        let list = [];
        if (res.code === 200) {
          list = res.data.map(item => {
            let cover = '';
            if (item.cover) cover = import.meta.env.VITE_CURRENT_ENV == 'dev' ? import.meta.env.VITE_DEV_BASE_SERVER + item.cover : serverUrl + item.cover;
            return {
              id: item.id,
              title: item.title,
              artist: item.artist,
              type: item.type,
              img: cover,
              src: import.meta.env.VITE_CURRENT_ENV == 'dev' ? import.meta.env.VITE_DEV_BASE_SERVER + item.src : serverUrl + item.src
            };
          });
          this.SET_MUSIC_LIST(list);
        }
      });
    }
  },

  persist: {
    enabled: true,
    strategies: [
      {
        key: '_LOCAL_APP',
        storage: localStorage,
        paths: ['isDark', 'articeDict', 'musicDict', 'isFullSreen', 'musicList', 'projectConfig', 'bgIndex', 'isAutoToggleBg', 'toggleBgTime'] //指定要长久化的字段
      }
    ]
  }
});
